<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script src='lib/webgl-utils.js'></script>
    <script src='lib/webgl-debug.js'></script>
    <script src='lib/cuon-utils.js'></script>
    <script src="lib/cuon-matrix.js"></script>
</head>
<body onload="main()">
<canvas id="mybox" width="500" height="500">
    请更换新版浏览器查看效果!
</canvas>
<script >
    let VSHADER_SOURCE=`
    attribute vec4 a_position;
    attribute vec4 a_color;
    varying vec4 v_color;
    uniform mat4 u_modelMatrix;
    void main(){
        gl_Position=u_modelMatrix*a_position;
        v_color=a_color;
    }`;
    let FSHADER_SOURCE=`
    precision mediump float;
    varying vec4 v_color;
    void main(){
    	gl_FragColor=v_color;
    }`
    let ANGLE_STEP=45.0;

    function main(){
        let canvas=document.getElementById('mybox')
        let gl=getWebGLContext(canvas)
        if(!initShaders(gl,VSHADER_SOURCE,FSHADER_SOURCE)){
            console.log('初始化shader失败');
            return;
        }
        let n=initVertexBuffers(gl)
        if(n<0){
            console.log('设置顶点位置失败')
            return;
        }
        //指定清空颜色
        gl.clearColor(0.0,0.0,0.0,1.0);
        let u_modelMatrix=gl.getUniformLocation(gl.program,'u_modelMatrix')
        let currentAngle=0.0;
        let modelMatrix=new Matrix4();

        let tick=function(){
            // 更新当前角度
            currentAngle=animate(currentAngle);
            // 绘制三角形
            draw(gl,n,currentAngle,modelMatrix,u_modelMatrix);
            // 请求浏览器调用tick函数，对浏览器发出一次请求，请求在未来的某一时刻调用tick,tick函数应该接受一个time参数用来表明此次调用的时间戳  P155
            requestAnimationFrame(tick);
        }

        tick();
    }

    function initVertexBuffers(gl){
        //向缓冲区传入坐标点和颜色的数据
        let vertices=new Float32Array([0.0,0.3,1.0,0.3,0.3,  -0.3,0.0,0.0,0.5,0.5,  0.3,0.0,0.2,0.0,1.0])
        let n=3;

        let vertexBuffer=gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer);
        gl.bufferData(gl.ARRAY_BUFFER,vertices,gl.STATIC_DRAW);

        let FSIZE=vertices.BYTES_PER_ELEMENT;

        let a_position=gl.getAttribLocation(gl.program,'a_position');
        gl.vertexAttribPointer(a_position,2,gl.FLOAT,false,FSIZE*5,0);

        let a_color=gl.getAttribLocation(gl.program,'a_color')
        gl.vertexAttribPointer(a_color,3,gl.FLOAT,false,FSIZE*5,FSIZE*2)

        gl.enableVertexAttribArray(a_position);
        gl.enableVertexAttribArray(a_color)
        return n;
    }
    //绘制三角形
    function draw(gl,n,currentAngle,modelMatrix,u_modelMatrix){
        modelMatrix.setRotate(currentAngle,0,0,1);
        gl.uniformMatrix4fv(u_modelMatrix,false,modelMatrix.elements);
        gl.clear(gl.COLOR_BUFFER_BIT)
        gl.drawArrays(gl.TRIANGLES,0,n)
    }

    let g_last=Date.now();
    //计算应该旋转的角度
    function animate(angle){
        let now=Date.now();
        let elapsed=now-g_last;
        g_last=now;
        let newAngle=angle+(ANGLE_STEP*elapsed)/1000.0
        return newAngle %=360
    }


</script>

</body>
</html>